package admin

import (
	"duoke/model"
	"duoke/service"
	"duoke/utils/gconv"
	"duoke/utils/gtime"
	"duoke/utils/orm"
	"duoke/utils/response"
	"fmt"
	"github.com/gin-gonic/gin"
)

var Order = new(orderCtl)

type orderCtl struct{}

func (ctl *orderCtl) List(c *gin.Context) {
	var req model.ReqList
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	where := "id > 0 "
	if req.Keyword != "" {
		where += "AND (user_id ='" + req.Keyword + "' OR order_no ='" + req.Keyword + "' OR nickname ='" + req.Keyword + "' OR name ='" + req.Keyword + "' OR phone ='" + req.Keyword + "' )"
	}
	if req.PayState != "" {
		where += "AND pay_state = '" + req.PayState + "' "
	}
	if req.State != "" {
		where += "AND hx_state = '" + req.State + "' "
	}
	if req.GroupState != "" {
		where += "AND group_state = '" + req.GroupState + "' "
	}
	if req.IsRobot != "" {
		where += "AND is_robot = '" + req.IsRobot + "' "
	}
	if req.GoodsId != "" {
		where += "AND goods_id = '" + req.GoodsId + "' "
	}
	if req.IsNewOld != "" {
		where += "AND is_new_old = '" + req.IsNewOld + "' "
	}
	if req.FromId != "" {
		where += "AND from_id = '" + req.FromId + "' "
	}
	if req.StaffId != "" {
		where += "AND staff_id = '" + req.StaffId + "' "
	}
	if req.TimeRange != nil && len(req.TimeRange) == 2 {
		startTime := gtime.StrToTime(req.TimeRange[0])
		endTime := gtime.StrToTime(req.TimeRange[1])
		where += "AND pay_time >= '" + gconv.String(startTime) + "' "
		where += "AND pay_time <= '" + gconv.String(endTime) + "' "
		fmt.Println(where)
	}
	if req.GroupNum != "" {
		where += "AND group_num = '" + req.GroupNum + "' "
	}
	if req.GroupMemberNum != "" {
		where += "AND group_member_num = '" + req.GroupMemberNum + "' "
	}
	list, err := service.Order.List(c, where, 10000, "")
	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	response.Success(c, "", list)
}
func (ctl *orderCtl) Page(c *gin.Context) {
	var req model.ReqList
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	where := "id > 0 "
	if req.Keyword != "" {
		where += "AND (order_no like'%" + req.Keyword + "%' OR nickname like'%" + req.Keyword + "%' OR name like'%" + req.Keyword + "%' OR phone like'%" + req.Keyword + "%' )"
	}
	if req.PayState != "" {
		where += "AND pay_state = '" + req.PayState + "' "
	}
	if req.State != "" {
		where += "AND hx_state = '" + req.State + "' "
	}
	if req.GroupState != "" {
		where += "AND group_state = '" + req.GroupState + "' "
	}
	if req.IsRobot != "" {
		where += "AND is_robot = '" + req.IsRobot + "' "
	}
	if req.GoodsId != "" {
		where += "AND goods_id = '" + req.GoodsId + "' "
	}
	if req.IsNewOld != "" {
		where += "AND is_new_old = '" + req.IsNewOld + "' "
	}
	if req.FromId != "" {
		where += "AND from_id = '" + req.FromId + "' "
	}
	if req.StaffId != "" {
		where += "AND staff_id = '" + req.StaffId + "' "
	}
	if req.TimeRange != nil && len(req.TimeRange) == 2 {
		startTime := gtime.StrToTime(req.TimeRange[0])
		endTime := gtime.StrToTime(req.TimeRange[1])
		where += "AND pay_time >= '" + gconv.String(startTime) + "' "
		where += "AND pay_time <= '" + gconv.String(endTime) + "' "
		fmt.Println(where)
	}
	if req.GroupNum != "" {
		where += "AND group_num = '" + req.GroupNum + "' "
	}
	if req.GroupMemberNum != "" {
		where += "AND group_member_num = '" + req.GroupMemberNum + "' "
	}
	list, total, err := service.Order.Page(c, req, where, "")
	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	response.Success(c, "", gin.H{"total": total, "list": list})
}

// 退款
func (ctl *orderCtl) Refund(c *gin.Context) {
	var req model.ReqId
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	order, err := service.Order.GetById(c, req.Id, "")
	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	if order.RefundState != 0 {
		response.Error(c, "该订单已退款过，请勿重复操作", nil)
		return
	}
	isSuccess, err := service.WechatPay.RefundV3(c, gconv.Float64(order.PayAmount), order.OrderNo, gconv.Float64(order.PayAmount))
	if err != nil {
		order.RefundMsg = err.Error()
		_, _ = orm.Db.ID(order.Id).Update(order)
		response.Error(c, err.Error(), nil)
		return
	}
	if isSuccess {
		order.RefundState = 1
		_, _ = orm.Db.ID(order.Id).Update(order)
		response.Success(c, "退款成功", nil)
		// 退还积分
		service.User.Score(c, order.UserId, -gconv.Int(order.PayAmount), "拼团退款退还积分")
		return
	} else {
		order.RefundMsg = "退款失败"
		_, _ = orm.Db.ID(order.Id).Update(order)
		response.Error(c, "退款失败", nil)
		return
	}
}
